<?php
namespace app\api\controller\v1\model\pet;

use think\Model;
use think\Db;


class PetInfo extends model
{
//显示宠物信息
    public function getShowUserPetinfo($user_id,$pageno){
        $pagesize = 3;
        $startno = $pageno * $pagesize;
        $result = Db::query("select a.pet_id,a.user_id,a.pet_name,a.pet_gender,a.pet_brith,a.pet_kind,a.pet_status,a.pet_icon,b.fans_count,c.pet_hungry_level from ym_pet as a join user_info as b on a.user_id = b.user_id join ym_pet_hungry as c on a.pet_id = c.pet_id where a.user_id = '$user_id' and a.permisstion_to_access = 1 ORDER BY a.pet_id desc limit {$startno},{$pagesize}");
        // var_dump($result);
        return $result;
    }


    //判断是否经过宠物认证
    public function getPermisstionToAccess($pet_id){
        $result = Db::query("select permisstion_to_access from ym_pet where pet_id = '$pet_id'");
        return $result;

    }

// 更新宠物状态
    public function updatePetStatus($pet_id,$pet_status){
         Db::query("update ym_pet set pet_status = '$pet_status' where pet_id = '$pet_id'");
        return 1;
    }

    // 宠物信息
    public function getPetManage($user_id){
        $result = Db::query("select pet_id,pet_name,pet_gender,pet_brith,pet_kind,pet_icon,permisstion_to_access from ym_pet where user_id = '$user_id'");
        return $result;
    }


    //    删除宠物
    public function deletePet($pet_id){
        Db::query("delete from ym_pet where pet_id = '$pet_id'");
        //删除这支宠物的饥饿值
        Db::query("delete from ym_pet_hungry where pet_id = '$pet_id'");
        return 1;
    }


//修改宠物资料
    public function getEditPetInfo($pet_id){
        $result = Db::query("select pet_name,pet_brith,pet_kind,pet_gender,pet_icon,img_url from ym_pet where pet_id = '$pet_id'");//点击修改资料，将此宠物信息显示
        return $result;
    }

    //查看连续喂养了多少天
    public function getContinueDay($day_list){
        // $time = date('Y-m-d');
        $continue_day = 1 ;//连续天数
        $length = count($day_list);

        if($length >= 1){
            for ($i=0; $i<=count($day_list); $i++){
                $timediff = strtotime(date('Y-m-d')) - strtotime($day_list[$i]['feed_time']);

                if((abs($timediff) / 86400) == $i){

                    $continue_day = $i+1;
                } else {
                    break;
                }
            }
        }
        return $continue_day;    //输出连续几天
    }


//显示宠物信息
    public function getPetInfo($user_id,$fP1Lat, $fP1Lon, $fP2Lat, $fP2Lon){

        $result = Db::query("select a.pet_id,a.user_id,a.pet_name,a.pet_brith,a.pet_kind,a.pet_gender,a.pet_icon,b.user_icon,b.fans_count,b.fosterage_or_not from ym_pet as a join user_info as b on a.user_id = b.user_id  where a.user_id = '$user_id' and a.permisstion_to_access = 1");
        $pet_id = array();
        foreach ($result as $key => $value) {
            $pet_id[] = $result[$key]['pet_id'];
            $lengh = count($pet_id);
            for ($i=0; $i < $lengh ; $i++) {
                $count = Db::query("select count(1) as count from ym_pet_appointment where pet_id = '$pet_id[$i]'");
                $result[$key]['appointment_count'] = $count[0]['count'];

                if ($count[0]['count'] <= 5) {
                    $user_icons = Db::table('ym_pet_appointment')->field('user_icon')->where('owned_user_id',$user_id)->where('pet_id',$pet_id[$i])->select();
                    $user_icon = $user_icons[$i]['user_icon'];
                }else{
                    $user_icons = Db::table('ym_pet_appointment')->field('user_icon')->where('owned_user_id',$user_id)->where('pet_id',$pet_id[$i])->limit(5)->select();

                }
            }
            $result[$key]['user_icons'] = $user_icons;
        }

        $distance = $this->calculate_distance($fP1Lat, $fP1Lon, $fP2Lat, $fP2Lon);
        $res = ['pet_info'=>$result,'distance'=>$distance];
        return $res;
    }






}